function [y, T, residual, g1] = dynamic_2(y, x, params, steady_state, sparse_rowval, sparse_colval, sparse_colptr, T)
residual=NaN(6, 1);
  T(1)=y(3)^params(4);
  T(2)=exp(y(17));
  T(3)=T(1)*T(2);
  T(4)=y(15)^(1-params(4));
  residual(1)=(y(11))-(T(3)*T(4));
  residual(2)=(y(14))-(y(13)-(1-params(3))*y(3));
  T(5)=y(12)^(-y(20));
  T(6)=y(22)^(1-y(30));
  T(7)=log(y(22));
  T(8)=(y(30)-1)^2;
  T(9)=params(4)*params(8)*params(1)*(T(6)*T(7)/(y(30)-1)+(T(6)-1)/T(8));
  T(10)=y(22)^(-y(30));
  T(11)=params(1)*T(10);
  residual(3)=(T(5)+T(9)*y(21)/y(13))-(T(11)*(1-params(3)+params(4)*y(21)/y(13)));
  residual(4)=(y(20))-(params(6)-params(8)*(y(11)-params(9)));
  residual(5)=(y(12)+y(14))-(y(11));
  T(12)=y(12)^(1-y(20));
  T(13)=log(y(12));
  T(14)=(y(20)-1)^2;
  T(15)=T(12)*T(13)/(y(20)-1)+(T(12)-1)/T(14);
  residual(6)=((params(4)-1)*params(8)*y(11)/y(15)*T(15))-(params(2)*y(15)^params(7)+(params(4)-1)*y(11)/y(15)*T(5));
  T(16)=getPowerDeriv(y(12),1-y(20),1);
  T(17)=getPowerDeriv(y(12),(-y(20)),1);
  T(18)=getPowerDeriv(y(22),1-y(30),1);
if nargout > 3
    g1_v = NaN(21, 1);
g1_v(1)=(-(T(4)*T(2)*getPowerDeriv(y(3),params(4),1)));
g1_v(2)=1-params(3);
g1_v(3)=(-(T(3)*getPowerDeriv(y(15),1-params(4),1)));
g1_v(4)=T(15)*(params(4)-1)*params(8)*(-y(11))/(y(15)*y(15))-(params(2)*getPowerDeriv(y(15),params(7),1)+T(5)*(params(4)-1)*(-y(11))/(y(15)*y(15)));
g1_v(5)=1;
g1_v(6)=1;
g1_v(7)=(-1);
g1_v(8)=T(9)*(-y(21))/(y(13)*y(13))-T(11)*params(4)*(-y(21))/(y(13)*y(13));
g1_v(9)=T(5)*(-T(13));
g1_v(10)=1;
g1_v(11)=(params(4)-1)*params(8)*y(11)/y(15)*(((y(20)-1)*T(13)*T(12)*(-T(13))-T(12)*T(13))/((y(20)-1)*(y(20)-1))+(T(14)*T(12)*(-T(13))-(T(12)-1)*2*(y(20)-1))/(T(14)*T(14)))-(params(4)-1)*y(11)/y(15)*T(5)*(-T(13));
g1_v(12)=T(17);
g1_v(13)=1;
g1_v(14)=(params(4)-1)*params(8)*y(11)/y(15)*((T(13)*T(16)+T(12)*1/y(12))/(y(20)-1)+T(16)/T(14))-(params(4)-1)*y(11)/y(15)*T(17);
g1_v(15)=1;
g1_v(16)=params(8);
g1_v(17)=(-1);
g1_v(18)=T(15)*(params(4)-1)*params(8)*1/y(15)-T(5)*(params(4)-1)*1/y(15);
g1_v(19)=y(21)/y(13)*params(4)*params(8)*params(1)*(((y(30)-1)*T(7)*T(6)*(-T(7))-T(6)*T(7))/((y(30)-1)*(y(30)-1))+(T(8)*T(6)*(-T(7))-(T(6)-1)*2*(y(30)-1))/(T(8)*T(8)))-(1-params(3)+params(4)*y(21)/y(13))*params(1)*T(10)*(-T(7));
g1_v(20)=y(21)/y(13)*params(4)*params(8)*params(1)*((T(7)*T(18)+T(6)*1/y(22))/(y(30)-1)+T(18)/T(8))-(1-params(3)+params(4)*y(21)/y(13))*params(1)*getPowerDeriv(y(22),(-y(30)),1);
g1_v(21)=T(9)*1/y(13)-T(11)*params(4)*1/y(13);
    if ~isoctave && matlab_ver_less_than('9.8')
        sparse_rowval = double(sparse_rowval);
        sparse_colval = double(sparse_colval);
    end
    g1 = sparse(sparse_rowval, sparse_colval, g1_v, 6, 18);
end
end
